package com.xxx.kjds.controller;

import com.xxx.kjds.entity.FinanceCheck;
import com.xxx.kjds.entity.TraceKey;
import com.xxx.kjds.entity.User;
import com.xxx.kjds.entity.Wallets;
import com.xxx.kjds.service.FinanceService;
import com.xxx.kjds.service.TraceService;
import com.xxx.kjds.service.UserService;
import com.xxx.kjds.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/finance")
public class FinaceController {
    @Autowired
    private FinanceService financeService;
    @Autowired
    private TraceService traceService;
    @Autowired
    private WalletService walletService;
    @Autowired
    private UserService userService;

    // 查询finance、trace、wallet三表，组装数据完整交易记录
    @PostMapping(value = "/getFinList")
    @ResponseBody
    public List<Object> getFinList(HttpServletResponse response) {
        //组装数据
        List<Object> resultList = new ArrayList<>();
        List<FinanceCheck> financeCheckList = financeService.getList();
        List<TraceKey> traceList = new ArrayList<>();
        for (FinanceCheck financeCheck : financeCheckList) {
            TraceKey tk = traceService.selectByFid(financeCheck.getId());
            Wallets wallets = walletService.selectByPrimaryKey(tk.getWalletsId());
            User user = userService.selectByPrimaryKey(wallets.getUserId());
            resultList.add(financeCheck);
            resultList.add(user);
        }
        return resultList;
    }

    @PostMapping(value = "/updateState")
    @ResponseBody
    public String updateState(HttpServletRequest request) {
        //获取前台传值
        Integer fid = Integer.valueOf(request.getParameter("id"));
        String uName = request.getParameter("uName");
        String type = request.getParameter("type");
        Double money = Double.valueOf(request.getParameter("money"));
        String state = request.getParameter("state");

        //根据fid获取一条完整finance记录
        FinanceCheck FinanceCheck = financeService.selectByPrimaryKey(fid);

        //根据state判断是否通过，是否需要改变wallet
        if (state.equals("不通过")) {
            FinanceCheck.setState("已审核(不通过)");
            financeService.updateByPrimaryKey(FinanceCheck);
            return "审核成功(不通过)!";
        }

        //根据uName找对应的wid
        User user = userService.selectByName(uName);
        Integer uid = user.getId();
        Wallets wallets = walletService.selectByUid(uid);

        //获取账户余额
        Double balance = wallets.getMoney();

        //根据type判断是充值还是取现
        if (type.equals("充值")) {
            wallets.setMoney((balance + money));
            int result = walletService.updateByPrimaryKey(wallets);
            if (result > 0) {
                FinanceCheck.setState("已审核(通过)");
            } else {
                FinanceCheck.setState("数据库请求失败!");
            }
            financeService.updateByPrimaryKey(FinanceCheck);
        } else if (type.equals("提现") && balance >= money) {
            wallets.setMoney(balance - money);
            int result = walletService.updateByPrimaryKey(wallets);
            if (result > 0) {
                FinanceCheck.setState("已审核(通过)");
            } else {
                FinanceCheck.setState("数据库请求失败!");
            }
            financeService.updateByPrimaryKey(FinanceCheck);
        }
        return "审核成功(通过)!";
    }

    /*@GetMapping(value = "/selectById{id}")
    public FinanceCheck selectById(@RequestParam("id") Integer id) {
        FinanceCheck FinanceCheck = financeService.selectByPrimaryKey(id);
        return FinanceCheck;
    }*/

    /*@GetMapping(value = "/selectAll")
    public List<FinanceCheck> selectAll() {
        FinanceCheckExample FinanceCheckExample = new FinanceCheckExample();
        FinanceCheckExample.Criteria criteria = FinanceCheckExample.createCriteria();
        criteria.andIdGreaterThan(0);
        List<FinanceCheck> list = financeService.selectByExample(FinanceCheckExample);
        return list;
    }*/

    //根据账户名查看流水记录,多表组装
    @PostMapping(value = "/selectByName")
    @ResponseBody
    public List<Object> selectByName(HttpServletRequest request) {
        //组装数据
        List<Object> result = new ArrayList<>();

        String userName = request.getParameter("name");
        // 根据用户名查user表，得到uid
        User user = userService.selectByName(userName);
        Integer uid = user.getId();

        // 根据uid查wallet表，得到wid
        Wallets wallets = walletService.selectByUid(uid);
        Integer wid = wallets.getId();

        // 根据wid查finance表，组装数据
        List<FinanceCheck> list = financeService.selectByWid(wid);
        for (FinanceCheck financeCheck : list) {
            result.add(financeCheck);
            result.add(user);
        }

        return result;
    }
}
